package com.sync.utils.project.wechat.agent.impl;

import com.alibaba.fastjson.JSON;
import com.sync.utils.common.enums.URLEnum;
import com.sync.utils.framework.okHttp.OkHttpUtils;
import com.sync.utils.project.wechat.agent.AuthAgent;
import com.sync.utils.project.wechat.pojo.AccessTokenReq;
import com.sync.utils.project.wechat.pojo.AccessTokenRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/5/6 15:13
 */
@Service
public class AuthAgentImpl implements AuthAgent {

    private final static Logger logger = LoggerFactory.getLogger(AuthAgentImpl.class);

    @Override
    public AccessTokenRes getAccessToken(AccessTokenReq request) {
        String rs = OkHttpUtils.builder().url(URLEnum.WX_GET_TOKEN.getUrl())
                // 有参数的话添加参数，可多个
                .addParam("grant_type", request.getGrantType())
                .addParam("appid", request.getAppid())
                .addParam("secret", request.getSecret())
                // 也可以添加多个
                .get()
                .sync();
        logger.info("网络查询结果：{}", rs);
        AccessTokenRes accessTokenRes = JSON.parseObject(rs, AccessTokenRes.class);
        return accessTokenRes;
    }
}
